﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccountUser = NC.DAL.AccountUser;


namespace NC.DAL
{
    public class AccountUserRepository: IAccountUserRepository
    {

        private NCEntities _ncEntities;
         public AccountUserRepository()
        {
            _ncEntities = new NCEntities();
        }

        public DataAccountUser RegisterAccountUser(DataAccountUser accountUser)
        {
           _ncEntities.AddToAccountUser(accountUser);   //.AddToAccountUsers(accountUser);
            _ncEntities.SaveChanges();
            return accountUser;
        }

        public DataAccountUser GetAccountUserByActivationCode(string activationCode)
        {
            return _ncEntities.AccountUser.FirstOrDefault(x => x.ActivationCode == activationCode);
        }
        public List<DataAccountUser> ValidateUser(string email, string password)
        {
             var accountdata=from d in _ncEntities.AccountUser.Where(x => x.Email == email && x.Password == password) select d;
             List<DataAccountUser> userdata = accountdata.ToList();
             return userdata;
        }
         
        /// <summary>
        /// To Check whether given email has been already registered or not
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool IsDuplicateEmail(string email)
        {
            var duplicateEmail = _ncEntities.AccountUser.FirstOrDefault(x => x.Email == email);
            return duplicateEmail != null;
        }

        public bool ActivateUser(int accountUserId)
        {
            try
            {
                var dataAccountUser = _ncEntities.AccountUser.FirstOrDefault(x => x.Id == accountUserId);
                if (dataAccountUser != null) 
                    dataAccountUser.Active = true;
                _ncEntities.SaveChanges();
                return true;
            }
            catch
            {
                 return false;
            }
        }
    }
}
